package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dto.UserSearchDto;
import com.heima.model.search.pojo.ApAssociateWords;
import com.heima.search.model.Trie;
import com.heima.search.service.ApAssociateWordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult findAssociate(UserSearchDto userSearchDto) {
        if (StringUtils.isBlank(userSearchDto.getSearchWords())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<ApAssociateWords> associateWords = this.mongoTemplate
                .find(Query.query(Criteria.where("associateWords")
                .regex(".*" + userSearchDto.getSearchWords() + ".*")), ApAssociateWords.class);
        return ResponseResult.okResult(associateWords);
    }

    /**
     * 联想词
     * @param userSearchDto
     * @return
     */
    public static volatile SoftReference<Trie> associateWordsTrie = null;
    @Override
    public ResponseResult findAssociateV2(UserSearchDto userSearchDto) {
        //1 参数检查
        if(userSearchDto == null || StringUtils.isBlank(userSearchDto.getSearchWords())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(associateWordsTrie==null || associateWordsTrie.get()==null){
            initTrieData();
        }
        Trie trie = associateWordsTrie.get();
        List<String> words = trie.getData(userSearchDto.getSearchWords());
        List<ApAssociateWords> apAssociateWordList = new ArrayList<>();
        if(words!=null && words.size()>0){
            for (int i = 0; i < words.size(); i++) {
                ApAssociateWords associateWords = new ApAssociateWords();
                associateWords.setAssociateWords(words.get(i));
                apAssociateWordList.add(associateWords);
            }
        }
        return ResponseResult.okResult(apAssociateWordList);
    }
    public void initTrieData() {
        Trie trie = new Trie();
        // 查询联想词
        List<ApAssociateWords> associateWords =
                mongoTemplate.findAll(ApAssociateWords.class);
        if (associateWords != null && associateWords.size() > 0) {
            // 将联想词  存入到字典树种
            associateWords.stream().map(ApAssociateWords::getAssociateWords).forEach(trie::insert);
        }
        // 字典树存放在内存， 使用软引用 在内存溢出前 会释放该内存
        associateWordsTrie = new SoftReference<Trie>(trie);
    }
    //@Override
    //public void afterPropertiesSet() throws Exception {
    //    // 当前Spring实例准备完毕后  执行的初始化方法
    //    System.out.println("=============初始化方法=============");
    //    initTrieData();
    //}
}
